<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>人力成本统计表(部门维度)</span>
      </div>

      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="统计区间">
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            size="small"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            重置
          </el-button>
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['work:report:dcost']"
          >
            导出
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="resList" stripe>
        <el-table-column label="序号" align="center" width="50">
          <template slot-scope="scope">
            {{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              scope.$index +
              1
            }}
          </template>
        </el-table-column>
        <el-table-column label="部门" prop="deptName" />
        <el-table-column label="工号" prop="userName" />
        <el-table-column label="姓名" prop="nickName" />
        <el-table-column label="职级职等" prop="level" />
        <el-table-column label="金额合计(元)" prop="amount" width="100" />
        <el-table-column label="工时合计(H)" prop="sumHour" width="100" />
        <el-table-column :label="item" v-for="item in headers" :key="item">
          <template slot-scope="scope">
            <span
              v-for="(item2, index2) in scope.row.projectCost"
              :key="index2"
            >
              {{ item2[item] }}
            </span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { getdcost, optionSelect as getProjects } from '@/api/work/report'
export default {
  name: 'DCost',
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 动态头
      headers: [],
      // 列表
      resList: [],
      // 日期选择
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      // 日期范围
      dateRange: ''
    }
  },
  created() {
    this.getFillProject()
  },
  methods: {
    /** 搜索 */
    getList() {
      if (this.dateRange && this.dateRange.length > 0) {
        this.dateRange = this.dateRange.map((e) => this.parseTime(e))
      }
      this.addDateRange(this.queryParams, this.dateRange)

      if (this.headers && this.headers.length > 0) {
        this.loading = true
        this.queryParams.params['pageNum'] = this.queryParams.pageNum
        this.queryParams.params['pageSize'] = this.queryParams.pageSize
        getdcost(this.queryParams).then((response) => {
          this.resList = response.rows
          this.total = response.total
          this.loading = false
        })
      }
    },
    getFillProject() {
      getProjects().then((response) => {
        this.headers = response.data.map((a) => a.projectCode)
        this.queryParams.projectCodeList = this.headers

        this.getList()
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = ''
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'work/report/dcostexport',
        {
          ...this.queryParams
        },
        `人力成本(部门维度)_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>

<style></style>
